import { _decorator, Component, find, Node, resources, Sprite, SpriteFrame, tween, v3 } from 'cc';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { UILayerMgr } from './UILayerMgr';
import { Start } from './Start';
import { UserInfo } from './UserInfo';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('RoleCellItem')
export class RoleCellItem extends Component {

    @property(Sprite)
    sp: Sprite;
    @property(Sprite)
    qualityCellBg: Sprite;
    @property(Node)
    isSelectIcon: Node;
    @property(Node)
    isGoBattleIcon: Node;

    role;

    init(data) {
        this.role=data;
        this.isSelectIcon.setPosition(0,-41,0);
        resources.load(`${data.roleTp.imgPath}/spriteFrame`,SpriteFrame,(err,res)=>{
            this.sp.spriteFrame=res;
        })
        resources.load(`qualityCellBg/${data.roleTp.quality}/spriteFrame`,SpriteFrame,(err,res)=>{
            this.qualityCellBg.spriteFrame=res;
        })

        if(StartSceneGameMgr.ins.roleSelectId==data.roleId){
            this.isSelectIcon.active=true;

            tween(this.isSelectIcon)
            .by(0.3,{position:v3(0,5,0)})
            .by(0.3,{position:v3(0,-5,0)})
            .union()
            .repeatForever()
            .start()
        }
        else{
            this.isSelectIcon.active=false;
        }

        if (data.roleId==UserInfo.currGoBattleRoleId) {
            this.isGoBattleIcon.active=true;
            
        }
        else{
            this.isGoBattleIcon.active=false;
        }

    }

    onclickThisCom() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        console.log("点击角色item")
        if (StartSceneGameMgr.ins.roleSelectId == this.role.roleId) {
            return;
        }
        StartSceneGameMgr.ins.roleSelectId = this.role.roleId;
        StartSceneGameMgr.ins.saveGameDate();
        UILayerMgr.getIns.resfreshUI("equipPanel");
        Start.ins.initHomePanel();

    }
}


